package lms.dwr.demo.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import lms.dwr.demo.common.DAOException;
import lms.dwr.demo.common.ServiceException;
import lms.dwr.demo.dao.DAODistrict;
import lms.dwr.demo.model.ModelDistrict;
import lms.dwr.demo.service.ServiceDistrict;
import lms.dwr.demo.utils.StringUtils;

public class ServiceDistrictImpl 
implements ServiceDistrict
{
	
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(ServiceDistrictImpl.class);
	
	/**
	 * The district repository
	 */
	private DAODistrict daoDistrict;
	
	/*
	 * (non-Javadoc)
	 * @see lms.dwr.demo.service.ServiceDistrict#getDistrictById(java.lang.String)
	 */
	public ModelDistrict getDistrictById (String id) throws ServiceException
	{
		try 
		{
			return this.daoDistrict.get(id);
		} 
		catch (DAOException e) 
		{
			LOGGER.error("It failed to fetch the district id:" + id);
		}
		catch (Exception e)
		{
			throw new ServiceException(e);
		}
		
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see lms.dwr.demo.service.ServiceDistrict#getSubDistrictsByParent(java.lang.String)
	 */
	public List<ModelDistrict> getSubDistrictsByParent (String parentId) throws ServiceException
	{
		ModelDistrict parent = new ModelDistrict();
		parent.setId(parentId);
		
		ModelDistrict entity = new ModelDistrict();
		entity.setParent(parent);
		
		return getDistrictByEntity(entity);
	}
	
	/*
	 * (non-Javadoc)
	 * @see lms.dwr.demo.service.ServiceDistrict#getDistrictByEntity(lms.dwr.demo.model.ModelDistrict)
	 */
	public List<ModelDistrict> getDistrictByEntity (ModelDistrict entity) throws ServiceException
	{
		try
		{
			DetachedCriteria criteria = DetachedCriteria.forClass(ModelDistrict.class);
			
			if (null != entity)
			{
				if (StringUtils.isNotEmpty(entity.getDistrictName()))
				{
					criteria.add(Restrictions.eq("districtName", entity.getDistrictName()));
				}
				
				if (entity.getParent() != null)
				{
					criteria.createCriteria("parent").add(Restrictions.eq("id", entity.getParent().getId()));
				}
				else
				{
					criteria.add(Restrictions.isNull("parent"));
				}
			}
			
			return this.daoDistrict.getListByCriteria(criteria);
		}
		catch(DAOException e)
		{
			LOGGER.error("It failed to fetch pagination of students!", e);
			return null;
		}
		catch (Exception e)
		{
			throw (new ServiceException("It failed to fetch pagination of students!", e));
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see lms.dwr.demo.service.ServiceDistrict#saveOrUpdate(lms.dwr.demo.model.ModelDistrict)
	 */
	@Override
	public void saveOrUpdate(ModelDistrict entity) throws ServiceException 
	{
		try 
		{
			this.daoDistrict.saveOrUpdate(entity);
		} 
		catch (DAOException e) 
		{
			LOGGER.error("It failed to save/update the district");
		}
	}
	
	public DAODistrict getDaoDistrict()
	{
		return daoDistrict;
	}

	public void setDaoDistrict(DAODistrict daoDistrict)
	{
		this.daoDistrict = daoDistrict;
	}
	
}
